package edu.gatech.cs2340.OregonTrail;

import java.awt.*;
import java.io.*;
import java.util.Map;

import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;

import edu.gatech.cs2340.OregonTrail.EventSystem.CommCoordinator;
import edu.gatech.cs2340.OregonTrail.EventSystem.AtLocationEvent;
import edu.gatech.cs2340.OregonTrail.EventSystem.AtRiverEvent;

import java.io.IOException;

/**
 * Panel for the game menu.
 * 
 * @author Thomas Mahoney
 */
public class MenuPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	//private OregonTrailApp parentFrame;
	//private JPanel panel;

	//private Party party;
	private Image backgroundImage;
	
	/**
	 * Create the panel.
	 * @throws IOException 
	 */
	public MenuPanel(final OregonTrailApp parentFrame, final CommCoordinator communicator, Party party) throws IOException {

		//this.party = party;
		backgroundImage = ImageIO.read(PlayPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/menupanel.png"));
		
		setPreferredSize(new Dimension(600, 425));
		setLayout(null);
		
		//change these based on gender
		JLabel lblplayerIcon = new JLabel("");
		lblplayerIcon.setBounds(35, 60, 46, 75);
		lblplayerIcon.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/tswift_icon.png")));
		add(lblplayerIcon);
		
		JLabel lblparty1icon = new JLabel("");
		lblparty1icon.setBounds(151, 60, 50, 75);
		lblparty1icon.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/hagrid_icon.png")));
		add(lblparty1icon);
		
		JLabel lblparty2Icon = new JLabel("");
		lblparty2Icon.setBounds(271, 60, 50, 75);
		lblparty2Icon.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/tswift_icon.png")));
		add(lblparty2Icon);
		
		JLabel lblparty3Icon = new JLabel("");
		lblparty3Icon.setBounds(391, 60, 50, 75);
		lblparty3Icon.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/hagrid_icon.png")));
		add(lblparty3Icon);
		
		JLabel lblparty4Icon = new JLabel("");
		lblparty4Icon.setBounds(511, 60, 50, 75);
		lblparty4Icon.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/tswift_icon.png")));
		add(lblparty4Icon);
		
		//names
		JLabel lblplayerName = new JLabel(party.getPlayer().getName());
		lblplayerName.setBounds(25, 140, 70, 14);
		add(lblplayerName);
		
		JLabel lblparty1Name = new JLabel(party.getParty().get(0).getName());
		lblparty1Name.setBounds(140, 140, 70, 14);
		add(lblparty1Name);
		
		JLabel lblparty2Name = new JLabel(party.getParty().get(1).getName());
		lblparty2Name.setBounds(260, 140, 70, 14);
		add(lblparty2Name);
		
		JLabel lblparty3Name = new JLabel(party.getParty().get(2).getName());
		lblparty3Name.setBounds(380, 140, 70, 14);
		add(lblparty3Name);
		
		JLabel lblparty4Name = new JLabel(party.getParty().get(3).getName());
		lblparty4Name.setBounds(500, 140, 70, 14);
		add(lblparty4Name);
		
		JLabel lblplayerStatus = new JLabel(party.getPlayer().getStatus().toString());
		lblplayerStatus.setBounds(25, 150, 70, 14);
		add(lblplayerStatus);
		
		JLabel lblparty1Status = new JLabel(party.getParty().get(0).getStatus().toString());
		lblparty1Status.setBounds(140, 150, 70, 14);
		add(lblparty1Status);
		
		JLabel lblparty2Status = new JLabel(party.getParty().get(1).getStatus().toString());
		lblparty2Status.setBounds(260, 150, 70, 14);
		add(lblparty2Status);
		
		JLabel lblparty3Status = new JLabel(party.getParty().get(2).getStatus().toString());
		lblparty3Status.setBounds(380, 150, 70, 14);
		add(lblparty3Status);
		
		JLabel lblparty4Status = new JLabel(party.getParty().get(3).getStatus().toString());
		lblparty4Status.setBounds(500, 150, 70, 14);
		add(lblparty4Status);
		
		Item item = new Item();
		item.setItemName("An ox");
		JLabel lblOxenlabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblOxenlabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblOxenlabel.setBounds(34, 301, 46, 14);
		add(lblOxenlabel);
		
		item.setItemName("A wagon wheel");
		JLabel lblWheelsLabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblWheelsLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblWheelsLabel.setBounds(94, 362, 46, 14);
		add(lblWheelsLabel);
		
		item.setItemName("A wagon axle");
		JLabel lblAxleslabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblAxleslabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblAxleslabel.setBounds(143, 284, 46, 14);
		add(lblAxleslabel);
		
		item.setItemName("A wagon tongue");
		JLabel lblTongueslabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblTongueslabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblTongueslabel.setBounds(211, 354, 46, 14);
		add(lblTongueslabel);
		
		item.setItemName("Food");
		JLabel lblFoodlabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblFoodlabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblFoodlabel.setBounds(274, 280, 46, 14);
		add(lblFoodlabel);
		
		item.setItemName("A set of clothing");
		JLabel lblClotheslabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblClotheslabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblClotheslabel.setBounds(327, 352, 46, 14);
		add(lblClotheslabel);
		
		item.setItemName("Ammunition, a box of 20 rounds");
		JLabel lblAmmoLabel = new JLabel(party.getPlayer().getInventory().getStock().get(item).toString());
		lblAmmoLabel.setHorizontalAlignment(SwingConstants.CENTER);
		lblAmmoLabel.setBounds(395, 285, 46, 14);
		add(lblAmmoLabel);
		
		JLabel lblLoadbutton = new JLabel("");
		lblLoadbutton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
			    try {
					BufferedReader in = new BufferedReader(new FileReader("saved.txt"));
				 
					/* need to implement the node for where you are*/
					
					/*
					 * Takes in
					 * 1. Party member names, status 
					 * 2. Inventory 
					 * 3. Player's breakc, money, name,sickc , status, 
					 * 4. Day
					 * 5. Milestraveled
					 */
					
					Game game = parentFrame.getGame();
					
					
					for(int i=0; i<4;i++)
					{
						game.getParty().getParty().get(i).setName(in.readLine());
						String str = in.readLine();
						if(str.equals(eStatus.alive))
						{
							game.getParty().getParty().get(i).setStatus(eStatus.alive);
						}
						if(str.equals(eStatus.sick))
						{
							game.getParty().getParty().get(i).setStatus(eStatus.sick);
						}
						if(str.equals(eStatus.dead))
						{
							game.getParty().getParty().get(i).setStatus(eStatus.dead);
						}
					}
					Object[]itemList = game.getParty().getPlayer().getInventory().getStock().keySet().toArray();
					Map<Item, Integer> playerStock = game.getParty().getPlayer().getInventory().getStock();
					for(int i=0; i<itemList.length;i++)
					{
						playerStock.put((Item)itemList[i], Integer.parseInt(in.readLine()));

					}

					in.readLine();
			
					game.getParty().getPlayer().setBreakC(Double.parseDouble(in.readLine()));
			
					game.getParty().getPlayer().setMoney(Integer.parseInt(in.readLine()));

					game.getParty().getPlayer().setName(in.readLine());

					game.getParty().getPlayer().setSickC(Double.parseDouble(in.readLine()));
					String str = in.readLine();
					if((str.equals(eStatus.alive)))
					{
						game.getParty().getPlayer().setStatus(eStatus.alive);
					}
					if((str.equals(eStatus.dead)))
					{
						game.getParty().getPlayer().setStatus(eStatus.dead);
					}
					if((str.equals(eStatus.sick)))
					{
						game.getParty().getPlayer().setStatus(eStatus.sick);
					}

					game.setDay(Integer.parseInt(in.readLine()));
					int milesTraveled = Integer.parseInt(in.readLine());
					game.getTrail().setMilesTraveled(milesTraveled);
					
					for(int i =0; i<game.getTrail().getLocations().length;i++)
					{
						game.getTrail().getLocations()[i].setVisited(Boolean.parseBoolean(in.readLine()));

					}
					for(int i =0; i<game.getTrail().getRiver().length;i++)
					{
						game.getTrail().getRiver()[i].setVisited(Boolean.parseBoolean(in.readLine()));
					}
					
					game.getTrail().setLocationIndex(Integer.parseInt(in.readLine()));
					game.getTrail().setRiverIndex(Integer.parseInt(in.readLine()));
					
					game.updatePlayPanel(parentFrame.getPlayPanel());
					Object currentLoc = game.getTrail().getCurrentLocation(milesTraveled);
					if (currentLoc != null)
					{
						if (currentLoc instanceof Location) communicator.ThrowGUIEvent(new AtLocationEvent((Location)currentLoc));
						else if (currentLoc instanceof River) communicator.ThrowGUIEvent(new AtRiverEvent((River)currentLoc));
						else JOptionPane.showMessageDialog(null, "Hey, we got an object that's neither null, river, nor location: " + currentLoc);
					}
			    } catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			    JOptionPane.showMessageDialog(null, "Game Loaded");
			    parentFrame.showMenu();
			}
		});
		lblLoadbutton.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/load_button.png")));
		lblLoadbutton.setBounds(479, 237, 100, 50);
		add(lblLoadbutton);
		
		JLabel lblSavebutton = new JLabel("");
		lblSavebutton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				File file = new File("saved.txt");
				try {
					Writer output = new BufferedWriter(new FileWriter(file));
					/* need to implement the node for where you are*/
					/*
					 * Takes in
					 * 1. Party member names, status 
					 * 2. Inventory 
					 * 3. Player's breakc, money, name,sickc , status, 
					 * 4. Day
					 * 5. Milestraveled
					 */
					
					Game game = parentFrame.getGame();
					
				
					for(int i=0; i<4;i++)
					{
						output.write(""+game.getParty().getParty().get(i).getName());
						output.write("\r\n");
						output.write(""+game.getParty().getParty().get(i).getStatus());
						output.write("\r\n");

					}
					Object[]itemList = game.getParty().getPlayer().getInventory().getStock().keySet().toArray();
					Map<Item, Integer> playerStock = game.getParty().getPlayer().getInventory().getStock();

					for(int i=0; i<itemList.length;i++)
					{
						output.write(""+playerStock.get(itemList[i]));
						output.write("\r\n");

					}
					output.write(""+game.getParty().getPlayer().toString());
					output.write("\r\n");

					output.write(""+game.getParty().getPlayer().getBreakC());
					output.write("\r\n");

					output.write(""+game.getParty().getPlayer().getMoney());
					output.write("\r\n");

					output.write(""+game.getParty().getPlayer().getName());
					output.write("\r\n");

					output.write(""+ game.getParty().getPlayer().getSickC());
					output.write("\r\n");

					output.write(""+game.getParty().getPlayer().getStatus());
					output.write("\r\n");

					
					output.write(""+game.getDay());
					output.write("\r\n");

					output.write(""+game.getTrail().getMilesTraveled());
					output.write("\r\n");
					
					for(int i =0; i<game.getTrail().getLocations().length;i++)
					{
						output.write(""+game.getTrail().getLocations()[i].getVisited());
						output.write("\r\n");

					}
					for(int i =0; i<game.getTrail().getRiver().length;i++)
					{
						output.write(""+game.getTrail().getRiver()[i].getVisited());
						output.write("\r\n");
					}
						output.write(""+game.getTrail().getLocationIndex());
						output.write("\r\n");

						output.write(""+game.getTrail().getRiverIndex());
						output.write("\r\n");


					output.close();
					JOptionPane.showMessageDialog(null, "Your game was saved");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
			}
		});
		lblSavebutton.setIcon(new ImageIcon(MenuPanel.class.getResource("/edu/gatech/cs2340/OregonTrail/Images/save_button.png")));
		lblSavebutton.setBounds(479, 306, 100, 50);
		add(lblSavebutton);
	}
	
		
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);

	    // Draw the background image.
	    g.drawImage(backgroundImage, 0, 0, null);
	}
}
